﻿using Dominio.Core.Business.Repository;
using NHibernate;
using System.Collections.Generic;
using NHibernate.Linq;
using System.Linq;

namespace Core.Infra.Repository {
    public class Repository<T> : IRepository<T> where T : class{

        private ISession _session;

        public Repository(ISession session) {
            _session = session;
        }

        #region IRepository<T> Members

        public void  Add(T entity)
        {
 	        _session.SaveOrUpdate(entity);
        }

        public void  Delete(T entity)
        {
            _session.Delete(entity);
        }

        public T  GetById(long id)
        {
 	        return _session.Get<T>(id);
        }

        public void  Change(T entity)
        {
 	        _session.SaveOrUpdate(entity);
        }

        public void  Save()
        {
            _session.Clear();
 	        _session.Flush();
            //_session.Close();
        }

        #endregion

        #region IRepository<T> Members


        public IList<T> GetAll() {
            return _session.Linq<T>().ToList();
        }

        #endregion
    }
}
